#!/sbin/openrc-run
# Copyright 1999-2017 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

RAILS_ENV=${RAILS_ENV:-production}
REDMINE_DIR=${REDMINE_DIR:-/var/lib/redmine}
REDMINE_ADDRESS=${REDMINE_ADDRESS:-localhost}
REDMINE_PORT=${REDMINE_PORT:-3000}
REDMINE_USER=${REDMINE_USER:-redmine}
REDMINE_GROUP=${REDMINE_GROUP:-redmine}
REDMINE_PIDFILE="/var/run/redmine/server.pid"

depend() {
	use git-daemon mysql net postgresql svnserve
}

start_pre() {
	if [ ! -e "${REDMINE_DIR}/config/initializers/secret_token.rb" ] ; then
		eerror "Execute the following command to initialize environment:"
		eerror
		eerror "# emerge --config www-apps/redmine"
		eerror
		return 1
	fi
	checkpath -q -d -m 0755 -o redmine:redmine /var/run/redmine
}

start() {
	ebegin "Starting redmine"
	start-stop-daemon --start --quiet \
		--user "${REDMINE_USER}:${REDMINE_GROUP}" \
		--pidfile "${REDMINE_PIDFILE}" \
		--exec /usr/bin/ruby "${REDMINE_DIR}"/bin/rails server -- \
		--daemon --environment="${RAILS_ENV}" \
		--binding="${REDMINE_ADDRESS}" --port="${REDMINE_PORT}" \
		--pid="${REDMINE_PIDFILE}" \
		${REDMINE_OPTS}
	eend $?
}

stop() {
	ebegin "Stopping redmine"
	start-stop-daemon --signal INT --quiet --pidfile "${REDMINE_PIDFILE}"
	eend $?
}
